package online.afeibaili.storebackendframework.server;

import online.afeibaili.storebackendframework.mapper.CouponMapper;
import online.afeibaili.storebackendframework.model.Coupon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//private double discount; // 优惠折扣
//private double reprice; // 优惠价格
//private double min; // 优惠最低价格
//private int quantity; // 优惠卷数量
//private long expireTime; // 到期时间
import java.util.Date;
import java.util.List;

@Service
public class CouponServer {
    @Autowired
    private CouponMapper couponMapper;

    public String insert(Coupon coupon) {
        String message;
        coupon.setCreateTime(new Date().getTime());
        if (coupon.getExpireTime()<coupon.getCreateTime()) {
            message = "到期时间不能早于创建时间";
            return message;
        }
        double discount = coupon.getDiscount();
        double reprice = coupon.getReprice();
        int isTrue = 0;
        if (reprice == 0 ^ discount == 0) {
            isTrue =  couponMapper.insert(coupon);
        }
        if (isTrue==0) {
            message = "添加失败";
        }else {
            message = "添加成功";
        }
        return message;
    }
    public List<Coupon> selectAll() {
        return couponMapper.selectAll();
    }
    public int getCount() {
        return couponMapper.getCount();
    }
}
